package org.tuoren.official.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.tuoren.common.core.utils.MapstructUtils;
import org.tuoren.common.mybatis.core.page.TableDataInfo;
import org.tuoren.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.tuoren.official.domain.Carousel;
import org.tuoren.official.domain.bo.TFloatingAdsBo;
import org.tuoren.official.domain.vo.TFloatingAdsVo;
import org.tuoren.official.domain.TFloatingAds;
import org.tuoren.official.mapper.TFloatingAdsMapper;
import org.tuoren.official.service.ITFloatingAdsService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 悬浮广告Service业务层处理
 *
 * @author Lion Li
 * @date 2025-05-26
 */
@RequiredArgsConstructor
@Service
public class TFloatingAdsServiceImpl implements ITFloatingAdsService {

    private final TFloatingAdsMapper baseMapper;

    /**
     * 查询悬浮广告
     *
     * @param id 主键
     * @return 悬浮广告
     */
    @Override
    public TFloatingAdsVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询悬浮广告列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 悬浮广告分页列表
     */
    @Override
    public TableDataInfo<TFloatingAdsVo> queryPageList(TFloatingAdsBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TFloatingAds> lqw = buildQueryWrapper(bo);
        Page<TFloatingAdsVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的悬浮广告列表
     *
     * @param bo 查询条件
     * @return 悬浮广告列表
     */
    @Override
    public List<TFloatingAdsVo> queryList(TFloatingAdsBo bo) {
        LambdaQueryWrapper<TFloatingAds> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TFloatingAds> buildQueryWrapper(TFloatingAdsBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TFloatingAds> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getPhotoUrl()), TFloatingAds::getPhotoUrl, bo.getPhotoUrl());
        lqw.eq(bo.getIsShow() != null, TFloatingAds::getIsShow, bo.getIsShow());
        lqw.eq(StringUtils.isNotBlank(bo.getLinkUrl()), TFloatingAds::getLinkUrl, bo.getLinkUrl());
        return lqw;
    }

    /**
     * 新增悬浮广告
     *
     * @param bo 悬浮广告
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(TFloatingAdsBo bo) {
        TFloatingAds add = MapstructUtils.convert(bo, TFloatingAds.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改悬浮广告
     *
     * @param bo 悬浮广告
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(TFloatingAdsBo bo) {
        TFloatingAds update = MapstructUtils.convert(bo, TFloatingAds.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TFloatingAds entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除悬浮广告信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }

    @Override
    public TableDataInfo<TFloatingAdsVo> frontList(TFloatingAdsBo bo) {
        LambdaQueryWrapper<TFloatingAds> lqw = Wrappers.lambdaQuery();
        lqw.eq(TFloatingAds::getIsShow,1);
        return TableDataInfo.build(baseMapper.selectVoList(lqw));
    }
}
